package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.AppManger;
import com.android.frame.util.DateTool;
import com.android.frame.util.Toolkit;
import com.android.frame.view.linearlistview.LinearListView;
import com.bigkoo.pickerview.TimePickerView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.PreReleaseTopicAdapter;
import com.kupurui.asteacher.adapter.PreRelesaeClassAdapter;
import com.kupurui.asteacher.bean.NewJobInfo;
import com.kupurui.asteacher.bean.PreReleaseInfo;
import com.kupurui.asteacher.bean.PushSuccessInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 预发布
 * Created by Administrator on 2017/10/18.
 */

public class PreReleaseAty extends BaseAty {


    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.et_homework_title)
    EditText etHomeworkTitle;
    @Bind(R.id.tv_end_time)
    TextView tvEndTime;
    @Bind(R.id.ll_end_time)
    LinearLayout llEndTime;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.listview)
    LinearListView listview;
    @Bind(R.id.tv_topic_num)
    TextView tvTopicNum;
    @Bind(R.id.tv_add_topic)
    TextView tvAddTopic;
    @Bind(R.id.listview_topic)
    LinearListView listviewTopic;
    @Bind(R.id.fbtn_submit)
    FButton fbtnSubmit;
    @Bind(R.id.fbtn_push)
    FButton fbtnPush;

    List<String> question;

    PreReleaseInfo preReleaseInfo;
    List<PreReleaseInfo.SchoolBean> classList;
    List<PreReleaseInfo.QuesBean> quesList;
    PreRelesaeClassAdapter classAdapter;
    PreReleaseTopicAdapter topicAdapter;

    private List<String> q_id;//发布时的ID
    private List<String> class_id;//班级ID
    private String title = "";
    private String issue_time = "";
    private String cut_time = "";

    private String type = "";//判断进入的地方

    @Override
    public int getLayoutId() {
        return R.layout.pre_release_aty;
    }

    @Override
    public void initData() {
        question = new ArrayList<>();
        classList = new ArrayList<>();
        quesList = new ArrayList<>();
        q_id = new ArrayList<>();
        class_id = new ArrayList<>();

        if (getIntent().getExtras() != null) {
            question = getIntent().getStringArrayListExtra("id");
            type = getIntent().getStringExtra("type");
        }

        if (type.equals("2")){
            tvAddTopic.setVisibility(View.GONE);
        }else {
            tvAddTopic.setVisibility(View.VISIBLE);
        }

        classAdapter = new PreRelesaeClassAdapter(this, classList, R.layout.job_push_item);
        listview.setAdapter(classAdapter);
//        listview.setOnItemClickListener(new LinearListView.OnItemClickListener() {
//            @Override
//            public void onItemClick(LinearListView parent, View view, int position, long id) {
//                if (classList.get(position).is_sel()) {
//                    classList.get(position).setIs_sel(false);
//                } else {
//                    classList.get(position).setIs_sel(true);
//                }
//                classAdapter.notifyDataSetChanged();
//            }
//        });
        topicAdapter = new PreReleaseTopicAdapter(this,quesList,R.layout.preview_radio_item);
        listviewTopic.setAdapter(topicAdapter);
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().zuoye_info(UserManger.getUserInfo().getId(),question,UserManger.yufabu_id,this,0);
    }

    @OnClick({R.id.iv_back,R.id.tv_add_topic,R.id.tv_end_time,R.id.fbtn_submit,R.id.fbtn_push})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.iv_back:
                UserManger.yufabu_id = "";
                finish();
                break;
            case R.id.tv_add_topic://添加题目
                UserManger.PUSH_TYPE = 1;
                for (int i = 0;i < quesList.size();i++){
                    UserManger.subject_id.add(quesList.get(i).getQuestion().getQ_id());
                }
                startActivity(JobArrangementAty.class,null);
                finish();
                break;
            case R.id.tv_end_time:
                TimePickerView timePickerView = new TimePickerView(this, TimePickerView.Type.ALL);
                timePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date) {
                        tvEndTime.setText(DateTool.dateToStr(date, "yyyy-MM-dd HH:mm"));
                        cut_time = DateTool.dateToStr(date,"yyyy-MM-dd HH:mm");
                    }
                });
                timePickerView.show();
                break;
            case R.id.fbtn_submit://保存
                issue_time = "保存";
                toReq(1);
                break;
            case R.id.fbtn_push:
                issue_time = "一键发布";
                toReq(2);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                preReleaseInfo = AppJsonUtil.getObject(result,PreReleaseInfo.class);
                classList.clear();
                classList.addAll(preReleaseInfo.getSchool());
                quesList.clear();
                quesList.addAll(preReleaseInfo.getQues());
                topicAdapter.notifyDataSetChanged();
                tvTopicNum.setText("已选题目("+quesList.size()+")");
                classAdapter.notifyDataSetChanged();

                if (!TextUtils.isEmpty(UserManger.yufabu_id)){
                    etHomeworkTitle.setText(preReleaseInfo.getInfo().getTitle());
                    tvEndTime.setText(preReleaseInfo.getInfo().getCut_time());
                    cut_time = preReleaseInfo.getInfo().getCut_time();
                }
                break;
            case 1:
                UserManger.subject_id.clear();
                UserManger.yufabu_id = "";
                showToast(AppJsonUtil.getResultInfo(result).getMessage());
                AppManger.getInstance().killActivity(PreviewAty.class);
                AppManger.getInstance().killActivity(JobSelAty.class);
                AppManger.getInstance().killActivity(SelKnowledgeAty.class);
                AppManger.getInstance().killActivity(JobArrangementAty.class);
                finish();
                startActivity(PreReleaseSuccessAty.class,null);
                break;
            case 2:
                UserManger.subject_id.clear();
                UserManger.yufabu_id = "";
                PushSuccessInfo pushSuccessInfo = AppJsonUtil.getObject(result,PushSuccessInfo.class);
                AppManger.getInstance().killActivity(PreviewAty.class);
                AppManger.getInstance().killActivity(JobSelAty.class);
                AppManger.getInstance().killActivity(SelKnowledgeAty.class);
                AppManger.getInstance().killActivity(JobArrangementAty.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean",pushSuccessInfo);
                startActivity(JobPushSuccessAty.class, bundle);
                finish();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void toReq(int what){
        title = etHomeworkTitle.getText().toString();
        if (TextUtils.isEmpty(title)){
            showToast("请输入作业名称");
            return;
        }
        if (TextUtils.isEmpty(cut_time)){
            showToast("请选择截止时间");
            return;
        }
        class_id.clear();
        for (int i = 0;i < classList.size();i++){
            if (classList.get(i).is_sel()){
                class_id.add(classList.get(i).getId());
            }
        }
        q_id.clear();
        for (int i = 0; i < quesList.size();i++){
            if (quesList.get(i).getIs_selector()){
                q_id.add(quesList.get(i).getQuestion().getQ_id());
            }
        }
        if (Toolkit.listIsEmpty(class_id)){
            showToast("请选择发布的班级");
            return;
        }
        if (Toolkit.listIsEmpty(q_id)){
            showToast("请选择发布的题目");
            return;
        }
        showLoadingDialog("");
        new Teacher().zuoye_fabu(UserManger.getId(),title,issue_time,cut_time,class_id,q_id,UserManger.yufabu_id,this,what);
    }

    @Override
    public void onBackPressed() {
        UserManger.yufabu_id = "";
        super.onBackPressed();
    }
}
